﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using School.Entities;
using School.Web.Models;
using School.Organization;

namespace School.Web.Controllers
{
    public class CampusController : Controller
    {
        //
        // GET: /Campus/

        public ActionResult Index()
        {
            var result = Structure.GetAllCampuses();
            List<CampusViewModel> model = new List<CampusViewModel>();

            foreach (ORG_Campus c in result)
            {
                model.Add(new CampusViewModel() { Id = c.Id, Name = c.Name, Description = c.Description });
            }

            return View(model);
        }

        //
        // GET: /Campus/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Campus/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Campus/Create

        [HttpPost]
        public ActionResult Create(CampusViewModel model)
        {
            try
            {
                Structure.AddCampus(new ORG_Campus() { Name = model.Name, Description = model.Description }); 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Campus/Edit/5
 
        public ActionResult Edit(long id)
        {
            var campus = Structure.GetCampus(id);
            CampusViewModel model = new CampusViewModel() { Id = campus.Id, Name = campus.Name, Description = campus.Description };
            return View(model);
        }

        //
        // POST: /Campus/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, CampusViewModel model)
        {
            try
            {
                Structure.UpdateCampus(new ORG_Campus() { Id = id, Name = model.Name, Description = model.Description }); 
                return RedirectToAction("Index");
            }
            catch
            {
                return View(model);
            }
        }

        //
        // GET: /Campus/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Campus/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
